<?php

defined ( '_JEXEC' ) or die ( 'Restricted access' );
$user = &JFactory::getUser ();
$db = &JFactory::getDBO (); 
class SJTabsHelper{
var $params;
function __construct($params){ 
}
function getModuleId($position){
$db = JFactory::getDBO();
$query = "SELECT id FROM #__modules WHERE position='$position' AND published=1 ORDER BY ordering";
$db->setQuery($query);
$rows = $db->loadObjectlist();
$lists = array();
$i=0;
foreach($rows as $row){
$lists[$i]->id = $row->id;
$i++;
}
return $lists;
}
function parseTabModuleById($moduleId){
$db = JFactory::getDBO();  
$modules = & $this->_load ();
$result ='';
foreach($modules as $module){
if(((int)$module->id == (int) $moduleId) && $module) $result = $module;				
}
return $result;
} 
function &_load()
{ 
global $app, $Itemid; 
static $modules;

if (isset($modules)) {
return $modules;
}

$user	=& JFactory::getUser();
$db		=& JFactory::getDBO(); 
$aid	= (!$user->get('aid', 1)) ? 1 : $user->get('aid', 1);

$modules	= array();

$wheremenu = isset( $Itemid ) ? ' AND ( mm.menuid = '. (int) $Itemid .' OR mm.menuid = 0 )' : ''; 
$query = 'SELECT id, IF(title="","","") as title , module, position, content, showtitle, params'
. ' FROM #__modules AS m'
. ' LEFT JOIN #__modules_menu AS mm ON mm.moduleid = m.id'
. ' WHERE m.published = 1'
. ' AND m.access <= '. (int)$aid
. ' AND m.client_id = '. (int)$app->getClientId()
. $wheremenu
. ' ORDER BY position, ordering'; 
$db->setQuery( $query );

if (null === ($modules = $db->loadObjectList())) {
JError::raiseWarning( 'SOME_ERROR_CODE', JText::_( 'Error Loading Modules' ) . $db->getErrorMsg());
return false;
}

$total = count($modules);
for($i = 0; $i < $total; $i++)
{
//determine if this is a custom module
$file					= $modules[$i]->module;
$custom 				= substr( $file, 0, 4 ) == 'mod_' ?  0 : 1;
$modules[$i]->user  	= $custom;
// CHECK: custom module name is given by the title field, otherwise it's just 'om' ??
$modules[$i]->name		= $custom ? $modules[$i]->title : substr( $file, 4 );
$modules[$i]->style		= null;
$modules[$i]->position	= strtolower($modules[$i]->position);
} 
return $modules;
}  
}
?>
